package interaction.servlet;
/**
* @author	gem
* 系统的数据
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import po.Book;
import service.impl.BookServiceImpl;
import util.Constant;


@WebServlet(description = "系统首页", urlPatterns = { "/home" })
public class home extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public home() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String returnString = getHomeContent(request);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append("Data={"+returnString+"}");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected String getHomeContent(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		BookServiceImpl books = new BookServiceImpl();
		
		Book b1 = new Book("1","2","3","4","5","6","7",0.0,0.1);
		Book b2 = new Book("21","22","23","24","25","26","27",0.0,0.1);
		List<Book> latestBooks = new ArrayList<>();
		latestBooks.add(b1);
		latestBooks.add(b2);
//		这里的数据都是模拟的，还需要填充
		//List<Book> tenBooks = books.getHighScoreBookList(0, 10);
		String latestBooksJson = JSONArray.toJSONString(latestBooks).toString();
		String hotestBooksJson = JSONArray.toJSONString(latestBooks).toString();
		String haopinBooksJson = JSONArray.toJSONString(latestBooks).toString();
		String tuijianJson = JSONArray.toJSONString(latestBooks).toString();
		String tags = Constant.getTags();
		String avatar = "avatar:'00.png'";
		String name = "name:'黑眼圈用户'";
		
		String resultJSONs = "latest:"+latestBooksJson
				+",hotest:"+hotestBooksJson
				+",haopin:"+haopinBooksJson
				+",tuijian:"+tuijianJson
				+","+tags
				+","+avatar+","+name;
		System.out.print(resultJSONs);
		return resultJSONs;
	}
}
